$(function() {
	$("#headTop").load("head.html");
	showFolder();

	$("#addFolder").click(addFold);
	$("#removeFolder").click(removeMenu);
	changeDivWidth();
	initMainHeight();
	// 当浏览器窗口大小改变时，设置显示内容的高度
	window.onresize = function() {
		changeDivWidth();
	}

});

function initMainHeight() {
	var h = document.documentElement.clientHeight;// 获取页面可见宽度
	document.getElementById("main").style.height = h - 53 + "px";
}

function changeDivWidth() {
	var w = document.documentElement.clientWidth;// 获取页面可见宽度
	document.getElementById("west").style.width = w * 0.14 + "px";
	document.getElementById("center").style.width = w * 0.53 + "px";
	document.getElementById("east").style.width = w * 0.20 + "px";
}

function showFolder() {

	$("#menu").tree({
		method : "POST",
		url : "menu/showMenuTree.do",
		onDblClick : function(node) {
			if("1"==node.id||"2"==node.id){
				messageShow("目录","系统默认目录,不允许修改!");
				return;
			}
			$("#menu").tree('beginEdit', node.target);
		},
		onAfterEdit : function(node) {
			renameMenu(node);
		},
		loadFilter : function(data, parent) {
			return data.data;
		}
	});

	$("#menu").tree({
		onLoadSuccess : function(node, data) {
			var root = $('#menu').tree('find', 1);
			$("#menu").tree('expand', root.target);

			// $("li").bind("mouseover", function(e) {
			// $(this).addClass('tree-node-selected');
			// $('#mm').menu('show', {
			// left: $(this)[0].offsetLeft+$(this)[0].offsetWidth-10,
			// top: $(this)[0].offsetTop
			// });
			// return false;
			// });
			// $("li").bind("mouseout", function(e) {
			// $(this).removeClass('tree-node-selected');
			// return false;
			// });

		}
	});

}

// 修改目录名称
function renameMenu(node) {
	
	var nodeId = node.id;
	var text = node.text;
	var datas = {
		"id" : nodeId,
		"text" : text,
		"parentId" : ""
	};
	updateMenu(datas, node, "RENAME");
}

// 增加目录
function addFold() {
	var selected = $('#menu').tree('getSelected');
	if (selected == null || selected.length == 0 || selected == undefined) {
		selected = $('#menu').tree('find', 1);
	}
	var nodeId = GenNonDuplicateID(3);
	var parentId = selected.id;
	var datas = {
		"id" : nodeId,
		"text" : "新目录",
		"parentId" : parentId
	};
	updateMenu(datas, selected, "ADD");
};
// 更新目录
function updateMenu(datas, selected, type) {
	$.ajax({
		url : 'menu/updateMenu.do',
		data : datas,
		type : 'post',
		dataType : 'json',
		success : function(result) {

			$("#menu").tree('reload');

			if ("ADD" == type) {
				$("#menu").tree({
					onLoadSuccess : function(node, data) {
						var root = $('#menu').tree('find', 1);
						$("#menu").tree('expand', root.target);

						var select = $('#menu').tree('find', datas.id);
						$("#menu").tree('expandTo', select.target);
						$('#menu').tree('select', select.target);

					}
				});
			}

		},
		error : function(e) {
			alert("通信失败!");
		}
	});
}

// 删除目录及其子目录
function removeMenu() {
	var selected = $('#menu').tree('getSelected');
	if (selected == null) {
		messageShow("目录", "未选择要删除的目录!");
		return;
	}

	var menuId = selected.id;
	var datas = {
		'menuId' : menuId
	}

	var btnFn = function() {
		$.ajax({
			url : 'menu/deleteMenus.do',
			data : datas,
			type : 'post',
			dataType : 'json',
			success : function(result) {
				$("#menu").tree('reload');
			},
			error : function(e) {
				alert("通信失败!");
			}
		});
		easyDialog.close();
		return false;
	};

	easyDialog.open({
		container : {
			header : '目录',
			content : '确定要删除目录及其所有子目录吗?',
			yesFn : btnFn,
			noFn : true
		}
	});
}

/**
 *  * 生成一个用不重复的ID  
 */
function GenNonDuplicateID(randomLength) {
	return Number(Math.random().toString().substr(3, randomLength) + Date.now())
			.toString(36)
}






